<template>
<div class="product-add">
    <!--form表单-->
    <el-form size="small" ref="form" :model="form" label-width="200px">
      <!--添加门店-->
      <div class="common-form">{{$t('setting.edit_receipt_printer')}}</div>
      <el-form-item :label="$t('setting.printer_name')" prop="printerName" :rules="[{  required: true,  message: ' '}]">
        <el-input v-model="form.printerName" class="max-w460"></el-input>
      </el-form-item>
      <el-form-item :label="$t('setting.printer_type')">
        <el-select v-model="form.printerType" :placeholder="$t('setting.please_select')">
          <el-option v-for="(item, index) in type" :key="index" :label="item" :value="item">
          </el-option>
        </el-select>
        <div class="tips">{{$t('setting.printer_support_note')}}</div>
      </el-form-item>


      <!-- 飞鹅打印机 -->
      <div v-if="form.printerType == $t('setting.feie_printer')" >
        <el-form-item label="USER" prop="feieYun.USER" :rules="[{  required: true,  message: ' '}]">
          <el-input v-model="form.feieYun.USER" class="max-w460"></el-input>
          <div class="tips">{{$t('setting.feie_username')}}</div>
        </el-form-item>

        <el-form-item label="UKEY" prop="feieYun.UKEY" :rules="[{  required: true,  message: ' '}]">
          <el-input v-model="form.feieYun.UKEY" class="max-w460"></el-input>
          <div class="tips">{{$t('setting.feie_ukey')}}</div>
        </el-form-item>

        <el-form-item :label="$t('setting.printer_sn')" prop="feieYun.SN" :rules="[{  required: true,  message: ' '}]">
          <el-input v-model="form.feieYun.SN" class="max-w460"></el-input>
          <div class="tips">{{$t('setting.printer_sn_tip_2')}}</div>
        </el-form-item>
      </div>

      <!-- 365云打印 -->
      <div v-if="form.printerType == $t('setting.cloud365_printer')">
        <el-form-item :label="$t('setting.printer_sn')" prop="printCenter.deviceNo" :rules="[{  required: true,  message: ' '}]">
          <el-input v-model="form.printCenter.deviceNo" class="max-w460"></el-input>
        </el-form-item>

        <el-form-item :label="$t('setting.cloud365_key')" prop="printCenter.key" :rules="[{  required: true,  message: ' '}]">
          <el-input v-model="form.printCenter.key"></el-input>
        </el-form-item>
      </div>
      <el-form-item :label="$t('setting.print_copies')" prop="printTimes" :rules="[{  required: true,  message: ' '}]">
        <el-input v-model="form.printTimes" type="number" class="max-w460"></el-input>
        <div class="tips">{{$t('setting.order_print_times')}}</div>
      </el-form-item>

      <el-form-item :label="$t('setting.sort_order')">
        <el-input v-model="form.sort" type="number" class="max-w460"></el-input>
        <div class="tips">{{$t('setting.sort_order_tip')}}</div>
      </el-form-item>

      <!--提交-->
      <div class="common-button-wrapper">
          <el-button type="primary" @click="onSubmit" :loading="loading">{{$t('setting.submit_button')}}</el-button>
      </div>



    </el-form>


  </div>
</template>

<script>
import SettingApi from '@/api/setting.js';
export default {
  data() {
    return {
      /*切换菜单*/
      // activeIndex: '1',
      /*form表单数据*/
      form: {
        printerId: '',
        printerName: '',
        printerType: '',
        sort: 1,
        printTimes: '',
        feieYun: {
          USER: '',
          UKEY: '',
          SN: ''
        },
        printCenter: {
          deviceNo: '',
          key: ''
        }
      },
      loading: false,
      type: []
    };
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      let self = this;
      // 取到路由带过来的参数
      const params = this.$route.query.printerId;
      SettingApi.printerDetail({
        printerId: params
      }, true).then(res => {
        let detail = res.data.printer;
        this.type = res.data.printerType;
        this.form.printerName = detail.printerName;
        this.form.printerType = detail.printerType;
        this.form.sort = detail.sort;
        this.form.printerId = detail.printerId;
        this.form.printTimes = detail.printTimes;
        if (detail.printerType == $t("setting.feie_printer")) {
          this.form.feieYun.USER = detail.printerConfig.USER;
          this.form.feieYun.UKEY = detail.printerConfig.UKEY;
          this.form.feieYun.SN = detail.printerConfig.SN;
        }
        if (detail.printerType == $t("setting.cloud365_printer")) {
          this.form.PRINT_CENTER.deviceNo = detail.printerConfig.deviceNo;
          this.form.PRINT_CENTER.key = detail.printerConfig.key;
        }
      }).catch(error => {});
    },
    //提交表单
    onSubmit() {
      let self = this;
      let form = self.form;
      self.$refs.form.validate(valid => {
        if (valid) {
          self.loading = true;
          SettingApi.editPrinter(form, true).then(data => {
            self.loading = false;
            ElMessage({
              message: $t("setting.edit_success_message"),
              type: 'success'
            });
            self.$router.push('/setting/printer/index');
          }).catch(error => {
            self.loading = false;
          });
        }
      });
    }
  }
};
</script>

<style>
.tips {
    color: #ccc;
  }
</style>

